package com.anjian.bamin.anjiandianzimap.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aiton.administrator.shane_library.shane.utils.PinyinUtils;
import com.anjian.bamin.anjiandianzimap.R;
import com.anjian.bamin.anjiandianzimap.model.CompanyInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MyCompanyAdapter extends BaseAdapter {

    private final HashMap<String, Integer> letterIndexes;
    private final String[] sections;
    private Context mContext;
    private List<CompanyInfo> mCompanyInfoList = new ArrayList<>();
    private LayoutInflater inflater;
    private OnCompanyClickListener onCompanyClickListener;

    public MyCompanyAdapter(Context mContext, List<CompanyInfo> mCompanyInfoList) {
        this.mContext = mContext;
        this.mCompanyInfoList = mCompanyInfoList;
        this.inflater = LayoutInflater.from(mContext);
        int size = this.mCompanyInfoList.size();
        letterIndexes = new HashMap<>();
        sections = new String[size];
        Log.e("MyCompanyAdapter", "MyCompanyAdapter: --->>" +size );
        for (int index = 0; index < size; index++) {
            //当前城市拼音首字母
            String currentLetter = PinyinUtils.getFirstLetter(mCompanyInfoList.get(index).getCompanyPinyin());
            //上个首字母，如果不存在设为""
            String previousLetter = index >= 1 ? PinyinUtils.getFirstLetter(mCompanyInfoList.get(index - 1).getCompanyPinyin()) : "";
            if (!TextUtils.equals(currentLetter, previousLetter)) {
                letterIndexes.put(currentLetter, index);
                sections[index] = currentLetter;
            }
            Log.e("MyCompanyAdapter", "MyCompanyAdapter: currentLetter--->>" +currentLetter );
            Log.e("MyCompanyAdapter", "MyCompanyAdapter: previousLetter--->>" +previousLetter );
        }
    }

    /**
     * 获取字母索引的位置
     *
     * @param letter
     * @return
     */
    public int getLetterPosition(String letter) {
        Integer integer = letterIndexes.get(letter);
        return integer == null ? -1 : integer;
    }

    @Override
    public int getCount() {
        return mCompanyInfoList == null ? 0 : mCompanyInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCompanyInfoList == null ? null : mCompanyInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            view = inflater.inflate(R.layout.item_companylist, parent, false);
            holder = new ViewHolder();
            holder.letter = (TextView) view.findViewById(R.id.tv_item_city_listview_letter);
            holder.name = (TextView) view.findViewById(R.id.textView_companyName);
            holder.address = (TextView) view.findViewById(R.id.textView_Address);
            holder.lineCompany = view.findViewById(R.id.line_company);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        final CompanyInfo companyInfo = mCompanyInfoList.get(position);
        String currentLetter = PinyinUtils.getFirstLetter(((CompanyInfo) companyInfo).getCompanyPinyin());
        String previousLetter = position >= 1 ? PinyinUtils.getFirstLetter(mCompanyInfoList.get(position - 1).getCompanyPinyin()) : "";
        if (!TextUtils.equals(currentLetter, previousLetter)) {
            holder.letter.setVisibility(View.VISIBLE);
            holder.letter.setText(currentLetter);
        } else {
            holder.letter.setVisibility(View.GONE);
        }
        final int mPosition = position;
        holder.lineCompany.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCompanyClickListener != null) {
                    onCompanyClickListener.onCompanyClick(companyInfo);
                }
            }
        });

        final String city = companyInfo.getCompanyName();
        holder.name.setText(city);
        String companyAddress = companyInfo.getCompanyAddress();
        holder.address.setText(companyAddress);

        return view;
    }

    private static class ViewHolder {
        TextView letter;
        TextView name;
        TextView address;
        View lineCompany;
    }

    public void setOnCompanyClickListener(OnCompanyClickListener listener) {
        this.onCompanyClickListener = listener;
    }

    public interface OnCompanyClickListener {
        void onCompanyClick(CompanyInfo companyInfo);
    }
}